# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=readline
pkgver=8.2.7
pkgrel=0
_myver=${pkgver%.*}
pkgdesc="GNU readline library"
url="https://tiswww.cwru.edu/php/chet/readline/rltop.html"
arch="all"
options="!check"
license="GPL-3.0-or-later"
makedepends_host="ncurses-dev chrpath"
subpackages="$pkgname-static $pkgname-dev $pkgname-doc libhistory"
source="https://ftp.gnu.org/gnu/readline/readline-$_myver.tar.gz
	fix-ncurses-underlinking.patch
	fix-rl_do_undo-crash.patch
	inputrc
	"
patch_args="-p0" # patchlevel used by upstream patches

_i=1
while [ $_i -le ${pkgver##*.} ]; do
	_patch=$(printf "%03d" $_i)
	_name=$pkgname${_myver//./}-$_patch
	source="$source
		$_name.patch::https://ftp.gnu.org/gnu/readline/readline-$_myver-patches/$_name"
	_i=$((_i+1))
done

builddir="$srcdir/$pkgname-$_myver"

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--enable-static \
		--enable-shared
	make
}

package() {
	make -C "$builddir" DESTDIR="$pkgdir" install
	# verify that it is not underlinked as upstream designed it
	if ! readelf -d "$pkgdir"/usr/lib/libreadline.so | grep -q 'NEEDED.*ncurses'; then
		error "readline needs to be linked against ncurses"
		return 1
	fi
	rmdir "$pkgdir"/usr/bin
	install -Dm644 "$srcdir"/inputrc "$pkgdir"/etc/inputrc
	chrpath -d "$pkgdir"/usr/lib/*.so.*
}

libhistory() {
	pkgdesc="GNU History Library"

	amove usr/lib/libhistory.so.*
}

sha512sums="
0a451d459146bfdeecc9cdd94bda6a6416d3e93abd80885a40b334312f16eb890f8618a27ca26868cebbddf1224983e631b1cbc002c1a4d1cd0d65fba9fea49a  readline-8.2.tar.gz
b8e2f9a5c9294b49d35c2e1bbde523f01390dd3c8729f3a78e79799a236515ec723af795ba91b0a662a30e8c31df0f63bc80771e82cd31b4673f41428a5049ae  fix-ncurses-underlinking.patch
ddb9308005996106c4bc369ed5726c2479aeb73287ef30a1dc4d6c54fad78f8b1ea46d89b75bd7b00c54acd0ee0d9d17debf922e2260caea2df212e2f9d5109e  fix-rl_do_undo-crash.patch
5dbe872e94166aaed7ca2edec5a34ef9b13b254381e252cc6d851877b461579903cbb5b5dc588eabececcf1ebe636f6cb4da406cd01b64757f8c7e7f62e9a276  inputrc
ea3800a62e992b3e594d78e77b6c4b662fb8c501b81010277e478656bb3b56db7747ba40c6c3d68c3ec5ab8e9e41be104788ec52268135d7e642ff40dea421a8  readline82-001.patch
2d4058c5c886498486d8514c238c0a05c0a97409011a7da40745e842977f0a29754e8b678f7e6e7a47c00df7e2e4c522d34d473118a24524af3824eed912e74a  readline82-002.patch
796df5a5bc4fc4077158621ac5359a7ccf81261e55880e6e70577ba24b50bd4fce4f2c70521d69a776f6ae8890d14d4f5c95cb9ba7c10f1065978c70626faeab  readline82-003.patch
dc5e4cb6b7a058f433c9c6a587772a4c63e0aeef9c59f45f3f859a007cdbaa46dfd560c43cfe51b9c5c0a3a4d662c100540afc6026b2f8544984867ea3b52661  readline82-004.patch
f20bcea5ce592edd0e41e0d725611ce536ceffc9ab176511ba6786ae802160b277c412ccbb2ce175da656bdfe5f0fd3f3ace11752cf1a152abff64c90f67a53a  readline82-005.patch
0a2737b2e566fafd60da8c0c7abcbaef24139165fb62a422d257b4cf38835b1e9b7a639c9e64228bbaac432d6a9ce62363b134284e55f3473df788f6433fbe63  readline82-006.patch
e96dafe28f20b1092af08422b9be99965e3a973a0c19f7c57669a6a7fbf74ad4cc3490fa2ad50a1845644a65b0a0e001cfc7ad29528b3b36827a1b9a2c9a96e7  readline82-007.patch
"
